package com.cloudsigns.factorymanager2.project.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.project.adapter.ProductListAdapter;
import com.cloudsigns.factorymanager2.project.bean.ProductBean;
import com.cloudsigns.factorymanager2.project.myinerface.ItemRemoveListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 添加工程信息
 * Created by tony on 2017/12/11.
 */

public class AddEngineeringInfomartionActivity extends Activity implements View.OnClickListener, ItemRemoveListener {
    private View rl_back;
    private TextView tv_title;
    private View img_add_product_list;
    private TextView btn_upload;
    private EditText edt__engineering_name, edt__engineering_address;
    private ListView lv;
    private ProductListAdapter adapter;
    private List<ProductBean> productBeanList = new ArrayList<>();
    private final String TAG = "AddEngineeringInfomartionActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_add_engineering_infomation);

        init();
    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        btn_upload = findViewById(R.id.btn_upload);
        img_add_product_list = findViewById(R.id.img_add_product_list);
        lv = findViewById(R.id.lv);
        edt__engineering_name = findViewById(R.id.edt__engineering_name);
        edt__engineering_address = findViewById(R.id.edt__engineering_address);

        tv_title.setText(getResources().getText(R.string.add_engineering_information));
        rl_back.setOnClickListener(this);
        img_add_product_list.setOnClickListener(this);
        btn_upload.setOnClickListener(this);

        adapter = new ProductListAdapter(this, TAG);
        adapter.setRemoveListener(this);
        adapter.setData(productBeanList);
        lv.setAdapter(adapter);
    }

    private void upload() {
        EngineeringInstallationOrderBean engineeringBean = new EngineeringInstallationOrderBean();
        engineeringBean.setEngineeringName(edt__engineering_name.getText().toString().trim());
        engineeringBean.setEngineeringAddress(edt__engineering_address.getText().toString().trim());
//        engineeringBean.setFinishDate(DateUtil.getMillis("2018-03-18"));
        engineeringBean.setLat(90);
        engineeringBean.setLon(180);
        List<String> picUrlLs = new ArrayList<>();
        picUrlLs.add("");
        engineeringBean.setPicUrl(picUrlLs);
        engineeringBean.setProductList(productBeanList);

        Intent mIntent = new Intent();
        mIntent.putExtra(ConstantUtil.ENGINEERING_INFO, engineeringBean);
        setResult(ConstantUtil.ADD_PRODUCT_LIST, mIntent);

        Toast.makeText(this, "上传成功", Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    public void remove(int positon) {  //列表删除事件
        if (positon >= 0 && positon < productBeanList.size()) {
            productBeanList.remove(positon);
            adapter.setData(productBeanList);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void remove(View view) {

    }

    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.img_add_product_list:
                mIntent = new Intent(AddEngineeringInfomartionActivity.this, AddProductInfomationActivity1.class);
                mIntent.putExtra("type","engineeringInfomartion");
                startActivityForResult(mIntent, ConstantUtil.ADD_PRODUCT_INFOMATION);

//                ProductBean productBean = new ProductBean();
//                productBean.setAmount(1);
//                productBean.setId("");
//                productBean.setName("batchproducttest1");
//                List<String> picUrlLs = new ArrayList<>();
//                picUrlLs.add("");
//                productBean.setPicUrl(picUrlLs);
//                productBean.setBorder(1);
//                productBean.setBorderBottom(1);
//                productBean.setBorderLeft(1);
//                productBean.setBorderRight(1);
//                productBean.setBorderTop(1);
//                productBean.setDesc("1*1*1");
//                productBean.setDiameter(1);
//                productBean.setHeight(1);
//                productBean.setThick(1);
//                productBean.setType(CommonEnum.ProductShapeType.ROUND);
//                productBean.setWidth(1);
//                productBean.setUnit("套");
//                productBeanList.add(productBean);
//                adapter.setData(productBeanList);
//                adapter.notifyDataSetChanged();
                break;
            case R.id.btn_upload:
                upload();
                break;

        }
    }
}
